package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AWaring;
import com.ruoyi.system.service.IAWaringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

/**
 * 告警设置Controller
 * 
 * @author ruoyi
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/lock/waring")
public class AWaringController extends BaseController
{
    @Autowired
    private IAWaringService aWaringService;

    /**
     * 查询告警设置列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:list')")
    @GetMapping("/list")
    public TableDataInfo list(AWaring aWaring)
    {
        startPage();
        List<AWaring> list = aWaringService.selectAWaringList(aWaring);
        return getDataTable(list);
    }

    /**
     * 导出告警设置列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:export')")
    @Log(title = "告警设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AWaring aWaring)
    {
        List<AWaring> list = aWaringService.selectAWaringList(aWaring);
        ExcelUtil<AWaring> util = new ExcelUtil<AWaring>(AWaring.class);
        util.exportExcel(response, list, "告警设置数据");
    }
    public static void main(String[] args) {
        // 服务器地址和端口
        String host = "192.168.1.234";
        int port = 8081;

        try (
                // 创建Socket连接
                Socket socket = new Socket(host, port);
                // 获取输出流，用于发送数据
                OutputStream outputStream = socket.getOutputStream();
        ) {
            // 要发送的消息
            String message = "684400190113F5919F3BB3C657";

            // 将字符串转换为字节数组
            byte[] messageBytes = message.getBytes();
            System.out.println(messageBytes.length);

//            while (true){
            for (int i = 0; i < 3; i++) {
                // 发送数据
                outputStream.write(messageBytes);

                System.out.println("Message sent to the server: " + message);
            }

//            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取告警设置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:query')")
    @GetMapping(value = "/getUserId")
    public AjaxResult getInfo()
    {
        return success(aWaringService.selectAWaringByUserId());
    }

    /**
     * 新增告警设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:add')")
    @Log(title = "告警设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AWaring aWaring)
    {
        return toAjax(aWaringService.insertAWaring(aWaring));
    }

    /**
     * 修改告警设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:edit')")
    @Log(title = "告警设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AWaring aWaring)
    {
        return toAjax(aWaringService.updateAWaring(aWaring));
    }

    /**
     * 删除告警设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:waring:remove')")
    @Log(title = "告警设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(aWaringService.deleteAWaringByUserIds(userIds));
    }
}
